<template>
  <div style="margin-top: 20px">
    <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input v-model="searchForm.keyword" placeholder="宠物名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.state" placeholder="审核状态">
          <el-option label="上架" value="1"></el-option>
          <el-option label="下架" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上架时间">
        <el-date-picker
            v-model="betweenTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="toQuery">查询</el-button>
        <el-button type="primary" @click="toAdd">新增</el-button>
        <el-button type="danger" @click="">批量删除</el-button>
        <el-button type="warning" @click="upOrDownDiaLog(1)">上架</el-button>
        <el-button type="default" @click="upOrDownDiaLog(0)">下架</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="tableData"
        border
        style="width: 100%;"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="resourcesStr"
          label="宠物图片">
        <template slot-scope="scope">
          <el-image :src="scope.row.resources"
                    :preview-src-list="[scope.row.resources]"></el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="name"
          label="宠物名称">
      </el-table-column>
      <el-table-column
          prop="state"
          label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0" type="info">下架</el-tag>
          <el-tag v-if="scope.row.state == 1" type="warning">上架</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄">
      </el-table-column>
      <el-table-column
          prop="saleprice"
          label="销售价格">
      </el-table-column>
      <el-table-column
          prop="costprice"
          label="成本价">
      </el-table-column>
      <el-table-column prop="petTypeName"
                       label="宠物类型">

      </el-table-column>
      <el-table-column
          prop="shopName"
          label="所属门店">
      </el-table-column>
      <el-table-column
          prop="onsaletime"
          label="上架时间">
      </el-table-column>

      <el-table-column label="操作" width="80px">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="toEdit(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchForm.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="searchForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>


    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%">
      <span>你确定<font color="red" size="20px">{{upDownFlag?"上":"下"}}架{{petNames}}</font>吗？</span>
      <span slot="footer" class="dialog-footer">
      <el-button @click="closeDiaLog()">取 消</el-button>
      <el-button type="primary" @click="updownPet()">确 定</el-button>
    </span>
    </el-dialog>

    <el-dialog  title="编辑宠物信息" :visible.sync="dialogPetFormVisible">
      <el-form ref="form" :model="petForm" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="petForm.name"></el-input>
        </el-form-item>
        <el-form-item label="宠物图片">
          <el-upload
              class="avatar-uploader"
              action="http://localhost/file/ossUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="销售价">
          <el-input v-model="petForm.saleprice"></el-input>
        </el-form-item>
        <el-form-item label="促销价">
          <el-input v-model="petForm.price"></el-input>
        </el-form-item>
        <el-form-item label="服务备注">
          <el-input type="textarea" v-model="petForm.detail.intro"></el-input>
        </el-form-item>
        <el-form-item label="服务详情">
          <quill-editor
              :options="editorOption"
              v-model="petForm.detail.adoptNotice"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="savePet()">保存</el-button>
        <el-button @click="dialogPetFormVisible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';

//去common文件夹找js文件，找quill-config.js
import quillConfig from '../../common/js/quill-config'

export default {
  name: "PetShengHe",
  components: { // 引入一个 自定义的components组件
    'quill-editor': quillEditor //QuillEditor  ---默认名称：quill-editor
  },
  data() {
    return {
      editorOption: quillConfig,
      betweenTime: null, //查询条件
      form: { //下面的3个属性，可省略
        id: '',//审核的数据的主键 ID
        title: '', //显示的标题
        note: '', //备注,
        state: null //审核状态
      },
      petForm: {
        detail: {}
      },//宠物编辑表单
     /* petForm: {
        "detail": {
          "intro": "哈哈哈",
          "adoptNotice": "<p><img src=\"https://tupi-demo.oss-cn-hangzhou.aliyuncs.com/1672898417109.png\"></p>"
        },
        "name": "123123",
        "saleprice": "10000",
        "price": "100",
        "resources": "https://tupi-demo.oss-cn-hangzhou.aliyuncs.com/1672898403256.png"
      },*/
      dialogPetFormVisible: false, //宠物编辑弹框
      dialogFormVisible: false, //宠物审核弹框
      total: 0, //总记录数
      multipleSelection: [],
      searchForm: {
        page: 1,
        size: 10
      },
      tableData: [],
      imageUrl:'',
      upDownFlag:1,
      petNames:"",
      dialogVisible:false

    }
  },
  methods: {
    closeDiaLog(){
      this.dialogVisible = false
    },
    savePet() {
      this.petForm.resources = this.imageUrl;
      this.$http.post('/pet', this.petForm).then(res => {
        let {success, message} = res.data;
        if (success) {
          this.dialogPetFormVisible = false;
          this.toQuery();
        }
        this.$message.info(message);
      })
    },
    toAdd() {
      this.petForm = {
        detail: {}
      }
      this.imageUrl = ""
      this.dialogPetFormVisible = true;
    },
    toEdit(row) {
      this.imageUrl = row.resources
      console.log(row)
      this.petForm = row;
      this.$http.post("/petDetail/findByPetId/"+row.id).then(res=>{
        console.log(res)
        this.petForm.detail = {
          id:res.data.id,
          intro:res.data.intro,
          adoptNotice:res.data.adoptNotice
        }
      })


      this.dialogPetFormVisible = true;

    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      /*if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }*/
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    upOrDownDiaLog(flag){

      var names = this.multipleSelection.map(item => item.name);

      if (!names || names.length === 0){
        this.$message.error("请选择有效数据-9");
        return;
      }

      this.upDownFlag = flag;
      this.petNames = names.join(",");

      this.dialogVisible = true
    },

    updownPet() {
      var ids = this.multipleSelection.map(item => item.id);

      let param = {
        ids :ids,
        state:this.upDownFlag
      }

      this.$http.post('/pet/upOrDownPet/', param).then(res => {
        let {message, success} = res.data;
        if (success) {
          this.dialogFormVisible = false;
          this.dialogVisible = false;
          this.toQuery();
        } else {
          this.$message.error(message);
        }
      })
    },
    toQuery() {
      if (this.betweenTime && this.betweenTime.length > 0) {//判断参数非空（!= '' && != underfind && != NAN）
        this.searchForm.betweenTime = this.betweenTime.join(",");
      } else {
        this.searchForm.betweenTime = ''
      }
      this.$http.post("/pet/findPage", this.searchForm).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleSizeChange(val) {
      this.searchForm.size = val;
      this.toQuery();
    },
    handleCurrentChange(val) {
      this.searchForm.page = val;
      this.toQuery();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  },
  mounted() {
    this.toQuery();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
